package day10.gec.线程通信3;

class Shower{
    int count = 1;

    public void show() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            synchronized (Object.class) {
                while (count != 1) {
                    Object.class.wait();
                }

                Thread.sleep(10);
                System.out.println("世界这么大");

                count = 2;
                Object.class.notify();
            }
        }
    }

    public void show2() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            synchronized (Object.class) {
                while (count != 2) {
                    Object.class.wait();
                }

                Thread.sleep(10);
                System.out.println("我想去看看");

                count = 1;
                Object.class.notify();
            }
        }
    }
}

public class ShowerDemo {
    public static void main(String[] args) {
        final Shower s = new Shower();
        new Thread() {
            @Override
            public void run() {
                try {
                    s.show();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                try {
                    s.show2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
